package com.ggh.jinjilive.ui.mall.goods.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemImageViewBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.huantansheng.easyphotos.models.album.entity.Photo;

import java.util.List;

public class AddImageGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> photoList;

    public void setAdd(List<String> photoList) {
        this.photoList = photoList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemImageViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_image_view, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
       ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
        String photo = photoList.get(position);
        if(null != photo){
            //图片
            Glide.with(holder.binding.itemImgPicture).load(photo).into(holder.binding.itemImgPicture);
            holder.binding.itemImgPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,photo);
                    }
                }
            });
        }else {
            holder.binding.itemImgPicture.setScaleType(ImageView.ScaleType.CENTER);
            Glide.with(holder.binding.itemImgPicture).load(R.mipmap.icon_add_goods_picture).into(holder.binding.itemImgPicture);
            holder.binding.itemImgPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(999999,photo);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return null == photoList ? 0 : photoList.size();
    }




    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemImageViewBinding binding;
        public ItemViewHolder(@NonNull ItemImageViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.itemImgPicture.getLayoutParams();

            params.height = (ScreenUtils.getScreenWidth() / 3) - SizeUtils.dp2px(30);
            binding.itemImgPicture.setLayoutParams(params);
        }
    }

}